/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;
import proyecto.modelo.Ficha_Medica;
import proyecto.dao.FichaDao;

/** 
 *
 * @author pgutierrez
 */
public class FichasController extends MultiActionController {

    public FichasController() {
        //Initialize controller properties here or 
        //in the Web Application Context
        //setCommandClass(MyCommand.class);
        //setCommandName("MyCommandName");
        //setSuccessView("successView");
        //setFormView("formView");
    }

    //@Override
    protected void doSubmitAction(Object command) throws Exception {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private FichaDao fichaDao;

    public FichaDao getFichaDao() {
        return fichaDao;
    }

    public void setFichaDao(FichaDao fichaDao) {
        this.fichaDao = fichaDao;
    }

    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        //request.setAttribute("usuarios", usuarioDAO.listar());
        //request.setAttribute("fichas", fichaDao.listar());
        //return new ModelAndView("ficha_medica");
                ModelAndView mv = new ModelAndView("ficha_medica");
        mv.addObject("fichas", fichaDao.listar());
        return mv;
    }

        public ModelAndView insertar(HttpServletRequest request, HttpServletResponse response) {
        //request.setAttribute("usuarios", usuarioDAO.listar());
        System.out.println("Entro a la llamada");
            //return new ModelAndView("ficha_medica");
                ModelAndView mv = new ModelAndView("ficha_medica");
        mv.addObject("fichas", fichaDao.listar());
        return mv;
    }

     public ModelAndView doInsertar(HttpServletRequest request, HttpServletResponse response) {
        //Usuario vo = new Usuario();
        //SimpleDateFormat df=new SimpleDateFormat("dd/mm/yyyy");
        System.out.println("Entro a doinsertar");
        Ficha_Medica fi=new Ficha_Medica();
        fi.setId_ficha_medica(Integer.parseInt(request.getParameter("Orden")));

        //try {
            //fi.setFec_registro(df.parse(request.getParameter("FecOrd")));
        //} catch (ParseException ex) {
            //Logger.getLogger(FichasController.class.getName()).log(Level.SEVERE, null, ex);
        //}
        fi.setFec_registro(request.getParameter("FecOrd"));

        fi.setEspecialidad(request.getParameter("Espcl"));
        fi.setPaciente(request.getParameter("Paciente"));
        fi.setAnalisis(request.getParameter("Analisis"));
        fi.setObservacion(request.getParameter("Observ"));
        System.out.println("Creo todo");
        fichaDao.insertar(fi);
        //request.setAttribute("fichas", fichaDao.listar());
        //ModelAndView mv = new ModelAndView(new RedirectView("ficha_medica.htm", true));
        ModelAndView mv = new ModelAndView("ficha_medica");
        mv.addObject("fichas", fichaDao.listar());
        return mv;
    }

    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request,
    HttpServletResponse response,
    Object command,
    BindException errors) throws Exception {
    ModelAndView mv = new ModelAndView(getSuccessView());
    //Do something...
    return mv;
    }
     */
}
